<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
function field_list_to_string($content_type)
{
	$str = array();
	if ($content_type->input_use_file_upload)
		$str[] = $content_type->label_file_upload;
	if ($content_type->input_use_url)
		$str[] = $content_type->label_url;
	if ($content_type->input_use_address)
		$str[] = $content_type->label_address;
	if ($content_type->input_use_subtitle)
		$str[] = $content_type->label_subtitle;
	if ($content_type->input_use_datetime)
		$str[] = $content_type->label_datetime;
	
	// push in "and" so you get: this, that, AND the other
	$c = count($str);
	if ($c > 1)
	{
		$c--;
		$str[$c] = "and {$str[$c]}";
	}
	
	return join(', ', $str);
}

function field_label($type)
{
	$str = "";
	if ($type == "input_use_file_upload")
		$str = "a file attachment";
	if ($type == "input_use_url")
		$str = "a URL";
	if ($type == "input_use_address")
		$str = "an address";
	if ($type == "input_use_subtitle")
		$str = "a block of text";
	if ($type == "input_use_datetime")
		$str = "a date and time";
	return $str;
}

function editable_field_list($content_type)
{
	$str = array();
	$str[] = "input_use_file_upload";
	$str[] = "input_use_url";
	$str[] = "input_use_address";
	$str[] = "input_use_subtitle";
	$str[] = "input_use_datetime";
	
	$output = "";
	if (count($str))
	{
		foreach ($str as $field)
		{
			$label = field_label($field);
			$klass = ($content_type->$field) ? ' checked="checked"' : '';
			$__label_field = str_replace('input_use', 'label', $field);
			$__label = $content_type->$__label_field;
			$output .= "<p><label for=\"{$field}_{$content_type->id}\"><input{$klass} type=\"checkbox\" id=\"{$field}_{$content_type->id}\" name=\"tpl[{$field}]\" value=\"1\" />
{$label}</label>
	&rarr; Label: <input type=\"text\" name=\"tpl[{$__label_field}]\" value=\"{$__label}\" /></p>\n";
		}
	}
	return $output;
}

// $type can be user, content
// $size is small, medium, large
function url_for_avatar($filename, $id, $type = 'user', $size = 'small', $env = APP_ENV)
{
	if ($filename && strlen($filename))
	{
		$hash = md5("{$type}_{$id}_{$size}_{$filename}");
		$ext  = end(explode('.', $filename));
		if ($env == 'production')
			$url = "http://static.bricabox.com/{$hash}.{$ext}";
		else
			$url = "http://s3.amazonaws.com/bricabox_{$env}/{$hash}.{$ext}";
	}
	else
		$url = "/assets/images/avatars/default_avatar_{$type}_{$size}.png";
	return $url;
}

function url_for_attachment($filename, $id, $env = APP_ENV)
{
	$hash = md5($filename);
	$ext  = end(explode('.', $filename));
	$filename = "{$hash}.{$ext}";
	if ($env == 'production')
		return "http://static.bricabox.com/attach_{$id}_{$filename}";
	else
		return "http://s3.amazonaws.com/bricabox_{$env}/attach_{$id}_{$filename}";
}

function size_to_attrs($size = 'small')
{
	$s = "";
	if ($size == 'small')
		$s = 25;
	if ($size == 'medium')
		$s = 50;
	if ($size == 'large')
		$s = 100;
	return "width=\"{$s}px\" height=\"{$s}px\"";
}

function username($user)
{
	// return the proper display name for $user using this logic:
	// 1. if user is an openid user with NO display name set, return their identity url with http:// stripped
	// 2. if user has a display name (regardless of being openid), use that
	// 3. if user doesn't have a display name, return their username
	if ($user->identity_url && (!strlen($user->display_name) || strtolower($user->display_name) == 'null'))
	{
		//return substr(preg_replace('/\/$/', '', $user->identity_url), 7); // strip http:// and trailing slash
		return 'New User';
	}
	if ($user->display_name && strtolower($user->display_name) != 'null')
		return $user->display_name;
	else
		return $user->login;
}

function user_url($user, $with_scope = false)
{
	global $BBX;
	$scope = $with_scope ? (isset($BBX['bricabox']) ? '/member/' : '/person/') : '';
	if ($user->login != $user->url_alias)
		return $scope . $user->url_alias;
	else
		return $scope . $user->login;
}

function h($str)
{
	return urlencode($str);
}

function hh($str)
{
	return htmlentities($str);
}

function bricabox_url($bricabox)
{
	if ($bricabox->use_custom_domain)
		return prep_url($bricabox->custom_domain);
	else
	{
		return preg_replace('/\:\/\//', "://{$bricabox->subdomain}.", APP_ROOT_URL);
	}
}

/**
 * Note: generates a RELATIVE url only
 */
$__CONTENTS = array();
function content_url($content)
{
	global $__CONTENTS;
	if (is_numeric($content) || !$content->id)
	{
		// TODO Load the content
	}
	return "/content/{$content->id}/" . url_title($content->field1_value);
}


/* Cache for activity_with_urls() */
$__DATA = array(
	'b' => array(),
	'u' => array()
);

// TODO: better way than $subject_as_they to toggle context (They vs. Kyle, this entry vs. Some Entry Title)
function activity_with_urls($data, $subject_as_they = false, $use_this = array())
{
	
	global $__DATA;
	
	$CI =& get_instance();
	//$CI->output->enable_profiler(true);
	
	// Get actor (user) data
	if ($data->actor_id && !isset($__DATA['u'][$data->actor_id]))
	{
		$__DATA['u'][$data->actor_id] = $CI->db->query('select * from users
			where id = ?', array($data->actor_id))->first_row();
	}
	
	// Handle activity_type
	switch ($data->activity_type)
	{
		case 'profile_comment':
		{
			
			if ($data->user_id && !isset($__DATA['u'][$data->user_id]))
			{
				$__DATA['u'][$data->user_id] = $CI->db->query('select * from users
					where id = ?', array($data->user_id))->first_row();
			}
			
			$subject = $subject_as_they ? 'They' : '<a href="' . user_url($__DATA['u'][$data->actor_id], true) .
				'">' . $data->subject . '</a>';
			$action  = 'commented on';
			$object  = '<a href="' . user_url($__DATA['u'][$data->user_id], true) .
				'">' . $data->object . "'s profile</a>";
		}
		break;
		
		case 'content_comment':
		{
			if ($data->bricabox_id && !isset($__DATA['b'][$data->bricabox_id]))
			{
				$__DATA['b'][$data->bricabox_id] = $CI->db->query('select name,use_custom_domain,custom_domain,subdomain
					from bricaboxes
					where id = ?', array($data->bricabox_id))->first_row();
			}
			
			$subject = $subject_as_they ? 'They' : '<a href="' . user_url($__DATA['u'][$data->actor_id], true) .
				'">' . $data->subject . '</a>';
			$action  = 'commented on';
			$object  	= !in_array('content', $use_this)
						? '<a href="' . bricabox_url($__DATA['b'][$data->bricabox_id])
							. '/content/' . $data->content_id . '">' . $data->object . '</a>'
						: 'this';
		}
		break;
		
		case 'add_tag':
		{
			if ($data->bricabox_id && !isset($__DATA['b'][$data->bricabox_id]))
			{
				$__DATA['b'][$data->bricabox_id] = $CI->db->query('select name,use_custom_domain,custom_domain,subdomain
					from bricaboxes
					where id = ?', array($data->bricabox_id))->first_row();
			}
			
			$subject = $subject_as_they ? 'They' : '<a href="' . user_url($__DATA['u'][$data->actor_id], true) .
				'">' . $data->subject . '</a>';
			$action  = 'tagged';
			$object  = "<a href=\"" . bricabox_url($__DATA['b'][$data->bricabox_id]) .
				"/content/{$data->content_id}\">" . $data->object . '</a>';
			$object .= ' with &quot;' . $data->extra_data . '&quot;';
		}
		break;
		
		case 'engagement':
		{
			
			if ($data->bricabox_id && !isset($__DATA['b'][$data->bricabox_id]))
			{
				$__DATA['b'][$data->bricabox_id] = $CI->db->query('select name,use_custom_domain,custom_domain,subdomain
					from bricaboxes
					where id = ?', array($data->bricabox_id))->first_row();
			}
			
			$ed		 = unserialize($data->extra_data);
			$subject = $subject_as_they ? 'They' : '<a href="' . user_url($__DATA['u'][$data->actor_id], true) .
				'">' . $data->subject . '</a>';
			$action  = $data->action == 'rated' ? '' : $data->action;
			$object  = !in_array('content', $use_this)
						? ($data->action == 'rated' ? " rated this a {$ed['v']}.0" : 'this')
							. ': <a href="' . bricabox_url($__DATA['b'][$data->bricabox_id])
								. '/content/' . $data->content_id . '">' . $data->object . '</a>'
						: ($data->action == 'rated' ? (' rated this a ' . $ed['v']) : ' on this');
		}
		break;
		
		case 'create_content':
		{
			
			if ($data->bricabox_id && !isset($__DATA['b'][$data->bricabox_id]))
			{
				$__DATA['b'][$data->bricabox_id] = $CI->db->query('select name,use_custom_domain,custom_domain,subdomain
					from bricaboxes
					where id = ?', array($data->bricabox_id))->first_row();
			}
			
			
			$subject 	= $subject_as_they ? 'They' : '<a href="' . user_url($__DATA['u'][$data->actor_id], true) .
				'">' . $data->subject . '</a>';
			$action		= !in_array('content', $use_this) ? " added a new <a href=\"" . bricabox_url($__DATA['b'][$data->bricabox_id]) .  "/entries/entry_type/{$data->extra_data}\">{$data->extra_data}</a> called " : 'added';
			$object  	= !in_array('content', $use_this)
						? '<a href="' . bricabox_url($__DATA['b'][$data->bricabox_id])
							. '/content/' . $data->content_id . '">' . $data->object . '</a>'
						: 'this';
		}
		break;
		
		case 'joined':
		{
			
			if ($data->bricabox_id && !isset($__DATA['b'][$data->bricabox_id]))
			{
				$__DATA['b'][$data->bricabox_id] = $CI->db->query('select name,use_custom_domain,custom_domain,subdomain
					from bricaboxes
					where id = ?', array($data->bricabox_id))->first_row();
			}
			
			
			$subject 	= $subject_as_they ? 'They' : '<a href="' . user_url($__DATA['u'][$data->actor_id], true) .
				'">' . $data->subject . '</a>';
			$action		= $data->action;
			$object		= !in_array('bricabox', $use_this)
							? '<a href="' . bricabox_url($__DATA['b'][$data->bricabox_id]) . '">'
									. $__DATA['b'][$data->bricabox_id]->name . '</a>'
							: 'this BricaBox';
		}
		break;
		
		case 'follow':
		{
			if ($data->user_id && !isset($__DATA['u'][$data->user_id]))
			{
				$__DATA['u'][$data->user_id] = $CI->db->query('select * from users
					where id = ?', array($data->user_id))->first_row();
			}
			
			$subject = $subject_as_they ? 'They are' : '<a href="' . user_url($__DATA['u'][$data->actor_id], true) .
				'">' . $data->subject . '</a> is';
			$action  = 'following';
			$object  = '<a href="' . user_url($__DATA['u'][$data->user_id], true) .
				'">' . $data->object . "</a>";
		}
		break;
		
		/*
		case 'twitter':
		{
			$subject = '<a href="http://twitter.com/' . $data->subject . '">@' . $data->subject . '</a>';
			$action  = 'twittered';
			$object  = "&quot;{$data->extra_data}&quot;";
		}
		break;
		*/
	}
	$str = $subject . ' ' . $action . ' ' . $object;
	return $str;
}

function wiki_text_css()
{
?>
<!-- Wiki CSS overrides -->
<style type="text/css" media="screen">
	<!--
	.wiki_text ul {
		list-style: inside square;
	}
	
	.wiki_text ol {
		list-style: inside decimal;
	}
	
	.wiki_text li {
		line-height: 18px;
	}
	
	.wiki_text b, .wiki_text strong {
		font-weight: bold;
	}
	
	.wiki_text i, .wiki_text em {
		font-style: italic;
	}
	-->
</style>
<?php
}
?>